from google.appengine.ext import db
from google.appengine.ext.db import polymodel

class GroupPermission(polymodel.PolyModel):
    """ This is the permission data model of every group member.
    All types of permissions are defined in this model.
    Common members have the basic permissions and admins have extra permissions
    to set the group settings.
    class properties:
    admin:      whether if the member is a administrator
    kick:       whether if the member can kick other members
    invite:     whether if the member can invite people
    ban:        whether if the member can ban other members
    mute:       whether if the member can mute other members
    warn:       whether if the member can give warnings to other members
    changeinfo: whether if the member can change the information of the group
    setop:      whether if the member can set a member as an operator or set an operator as a common user 
    closegroup: whether if the member can close group temporarily
    editcmd:    whether if the member can modify the commands of the group
    """
    admin=db.BooleanProperty(required=True)
    kick=db.BooleanProperty(required=True)
    invite=db.BooleanProperty(required=True)
    ban=db.BooleanProperty(required=True)
    mute=db.BooleanProperty(required=True)
    warn=db.BooleanProperty(required=True)
    changeinfo=db.BooleanProperty(required=True)
    setop=db.BooleanProperty(required=True)
    closegroup=db.BooleanProperty(required=True)
    editcmd=db.BooleanProperty(required=True)
    
class GroupMember(polymodel.PolyModel):
    """ Data model for all group members.
    account:      google account information
    jid:          the email of a member,must be a valid email address
    nick:         the nickname a member uses in the group,must be a valid nickname
    warning:      an integer,in range of 0 to 10,will be banned when reached 10
    offline_recv: always receive messages from group even when the member is offline
    blocklist:    the list of ids to be blocked by a member,10 at most
    lastspeak:    when the last time the member spoke in the group
    blocked:      whether if the member is blocked
    permission:   the member's permission record
    """
    account=db.UserProperty(required=True)
    jid=db.StringProperty(required=True)
    nick=db.StringProperty(required=True)
    warning=db.IntegerProperty()
    offline_recv=db.BooleanProperty(required=True)
    blocklist=db.StringListProperty()
    lastspeak=db.FloatProperty()
    blocked=db.BooleanProperty(required=True)
    permission=db.ReferenceProperty(GroupPermission)

class GroupOperator(GroupMember):
    founder=db.BooleanProperty()

class GroupCommand(polymodel.PolyModel):
    """ This is the group command data model.
    Every group command needs registration before use.
    Every GroupCommand entry contains all the information of a command,and every time a member
    send a command to the group,the bot will do a data query.
    Members:
    name:         string(20),the name of the command,used to distinguish commands
    prefix:       string(2),the prefix used by the command,all available prefixes are defined in the config file
    identifier:   string(15),the string right behind the prefix character,used to determine which command a member send
    info:         string(256),a summary of the command,used to explain what the command will do
    parameter:    integer,the number of parameters that the command should receive
    authorized:   list,what type of member can use this command
    
    """
    name = db.StringProperty()
    prefix=db.StringProperty()
    identifire=db.StringProperty()
    info=db.StringProperty(multiline=True)
    parameter=db.IntegerProperty()
    authorized=db.StringListProperty()

class DefaultMemberPermission(GroupPermission):
    admin=False
    kick=False
    invite=True
    ban=False
    mute=False
    warn=True
    changeinfo=False
    setop=False
    closegroup=False
    editcmd=False

class DefaultOperatorPermission(GroupPermission):
    admin=True
    kick=True
    invire=True
    ban=True
    mute=True
    warn=True
    changeinfo=True
    closegroup=False
    editcmd=False

        